package com.example.freeengine.wallpaper.persistence.repository;

import android.content.Context;

import androidx.lifecycle.LiveData;

import com.example.freeengine.constants.FreeEngineConstant;
import com.example.freeengine.wallpaper.persistence.dao.WallPaperCycleRefreshAlbumDao;
import com.example.freeengine.wallpaper.persistence.dao.WallPaperCycleRefreshAlbumItemDao;
import com.example.freeengine.wallpaper.persistence.database.WallPaperCycleRefreshDatabase;
import com.example.freeengine.wallpaper.persistence.entity.WallPaperCycleRefreshAlbum;
import com.example.freeengine.wallpaper.persistence.entity.WallPaperCycleRefreshAlbumItem;
import com.example.freeengine.wallpaper.persistence.factory.FreeEngineDatabaseFactory;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


/**
 * @author Terry Deng
 */
public class WallPaperCycleRefreshRepository {

    private WallPaperCycleRefreshDatabase wallPaperCycleRefreshDatabase;
    private WallPaperCycleRefreshAlbumDao wallPaperCycleRefreshAlbumDao;
    private WallPaperCycleRefreshAlbumItemDao wallPaperCycleRefreshAlbumItemDao;

    public WallPaperCycleRefreshRepository(Context context) {
        this.wallPaperCycleRefreshDatabase = FreeEngineDatabaseFactory.getWallPaperCycleRefreshDatabaseInstance(context, FreeEngineConstant.dbName);
        this.wallPaperCycleRefreshAlbumDao = this.wallPaperCycleRefreshDatabase.wallPaperCycleRefreshAlbumDao();
        this.wallPaperCycleRefreshAlbumItemDao = this.wallPaperCycleRefreshDatabase.wallPaperCycleRefreshAlbumItemDao();
    }

    public Long insertAlbum(WallPaperCycleRefreshAlbum wallPaperCycleRefreshAlbum) throws InterruptedException, ExecutionException, TimeoutException {
        return wallPaperCycleRefreshAlbumDao.insert(wallPaperCycleRefreshAlbum).get(2, TimeUnit.SECONDS);
    }
    public Integer updateAlbum(WallPaperCycleRefreshAlbum wallPaperCycleRefreshAlbum) throws InterruptedException, ExecutionException, TimeoutException {
        return wallPaperCycleRefreshAlbumDao.update(wallPaperCycleRefreshAlbum).get(2, TimeUnit.SECONDS);
    }
    public LiveData<List<WallPaperCycleRefreshAlbum>> findAlbumAll(){
        return wallPaperCycleRefreshAlbumDao.findAll();
    }
    public Integer deleteAlbum(WallPaperCycleRefreshAlbum wallPaperCycleRefreshAlbum) throws InterruptedException, ExecutionException, TimeoutException {
        return wallPaperCycleRefreshAlbumDao.delete(wallPaperCycleRefreshAlbum).get(2, TimeUnit.SECONDS);
    }



    public LiveData<Integer> countByAlbumId(Long id){
        return wallPaperCycleRefreshAlbumItemDao.countByAlbumId(id);
    }
    public LiveData<List<WallPaperCycleRefreshAlbumItem>> findAllByAlbumIdLiveData(Long id) {
        return wallPaperCycleRefreshAlbumItemDao.findAllByAlbumIdLiveData(id);
    }
    public List<WallPaperCycleRefreshAlbumItem> findAllByAlbumId(Long id) throws InterruptedException, ExecutionException, TimeoutException {
        return wallPaperCycleRefreshAlbumItemDao.findAllByAlbumId(id).get(2, TimeUnit.SECONDS);
    }
    public List<Long> insertAlbumItem(List<WallPaperCycleRefreshAlbumItem> wallPaperCycleRefreshAlbumItemList) throws InterruptedException, ExecutionException, TimeoutException {
        return wallPaperCycleRefreshAlbumItemDao.insert(wallPaperCycleRefreshAlbumItemList).get(2, TimeUnit.SECONDS);
    }
    public Integer deleteAlbumItem(WallPaperCycleRefreshAlbumItem wallPaperCycleRefreshAlbumItem) throws InterruptedException, ExecutionException, TimeoutException {
        return wallPaperCycleRefreshAlbumItemDao.delete(wallPaperCycleRefreshAlbumItem).get(2, TimeUnit.SECONDS);
    }



}
